package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BGreenCertificate;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BGreenCertificateMapper;
import com.isdepci.project.carbon.service.IBGreenCertificateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 购买绿证Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BGreenCertificateServiceImpl implements IBGreenCertificateService
{
    @Autowired
    private BGreenCertificateMapper bGreenCertificateMapper;


    @Override
    public AjaxResult calGreenCertificate(Long projectId) {
        // 查询所有BGreenCertificate数据
        List<BGreenCertificate> bGreenCertificates = bGreenCertificateMapper.selectBGreenCertificateByProjectId(projectId);
        // 初始化总的绿证碳汇量
        BigDecimal totalGreenCertificate = BigDecimal.ZERO;
        // 遍历每一条数据，计算购买绿证碳汇量
        for (BGreenCertificate bGreenCertificate : bGreenCertificates) {
            // 计算每条数据的购买绿证碳汇量
            BigDecimal greenCertificateValue = bGreenCertificate.getGreenCertElectricity()
                    .multiply(bGreenCertificate.getEmissionFactor());

            bGreenCertificate.setCarbonEmissions(greenCertificateValue);
            bGreenCertificateMapper.updateBGreenCertificate(bGreenCertificate);
            // 累加到总的碳汇量
            totalGreenCertificate = totalGreenCertificate.add(greenCertificateValue);
        }
        // 返回总的绿证碳汇量
        return AjaxResult.success(totalGreenCertificate.setScale(2, RoundingMode.HALF_UP));
    }

    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BGreenCertificate> list =  excel.getGreenCertificateList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bGreenCertificateMapper.insertList(list);
        }
        return AjaxResult.success();
    }


    /**
     * 查询购买绿证
     */
    @Override
    public BGreenCertificate selectBGreenCertificateById(Long id)
    {
        return bGreenCertificateMapper.selectBGreenCertificateById(id);
    }

    /**
     * 查询购买绿证列表
     */
    @Override
    public List<BGreenCertificate> selectBGreenCertificateList(BGreenCertificate bGreenCertificate)
    {
        return bGreenCertificateMapper.selectBGreenCertificateList(bGreenCertificate);
    }

    /**
     * 新增购买绿证
     */
    @Override
    public int insertBGreenCertificate(BGreenCertificate bGreenCertificate)
    {
        return bGreenCertificateMapper.insertBGreenCertificate(bGreenCertificate);
    }

    /**
     * 修改购买绿证
     */
    @Override
    public int updateBGreenCertificate(BGreenCertificate bGreenCertificate)
    {
        return bGreenCertificateMapper.updateBGreenCertificate(bGreenCertificate);
    }

    /**
     * 批量删除购买绿证
     */
    @Override
    public int deleteBGreenCertificateByIds(Long[] ids)
    {
        return bGreenCertificateMapper.deleteBGreenCertificateByIds(ids);
    }

    /**
     * 删除购买绿证信息
     */
    @Override
    public int deleteBGreenCertificateById(Long id)
    {
        return bGreenCertificateMapper.deleteBGreenCertificateById(id);
    }


}
